
$(function() {

	// $("#option").hide();
	$(".task-edit button").hide();
	//$("#edit-task-type").attr("readonly","readonly");
	$("#edit-task-name").attr("readonly","readonly");
	var editorTaskEdit = new wangEditor('editor-task');
	var editorAnswerEdit = new wangEditor('editor-answer');
	$("#edit-task-btn").click(function() {
		var count = $("#count").val();
		console.log(count);
		var edit = document.getElementById("edit-task-btn").value;
		if(edit=="保存") {
			//保存界面
			$("#edit-task-type").hide();
			$("#task-type").show();
			var taskEdit = editorTaskEdit.$txt.html();
			var answerEdit = editorAnswerEdit.$txt.html();
			editorTaskEdit.destroy();
			editorAnswerEdit.destroy();
			document.getElementById("edit-task-btn").value = "编辑";
			$(".taskEdit button").hide();
			$(".taskEdit a").show();
			$("#download-url").show();
			$("#edit-task-name").attr("contenteditable",false);
			$("#edit-task-name").css("border","0");
			$("#task-full-mark").attr("contenteditable",false);
			$("#task-full-mark").css("border","0");

			$("input[name=scoreItem]").addClass("readonly");
			$("input[name=scoreItem]").attr("readonly",true);
			$("input[name=scoreItemWeight]").addClass("readonly");
			$("input[name=scoreItemWeight]").attr("readonly",true);
			$("input[name=weight]").addClass("readonly");
			$("input[name=weight]").attr("readonly",true);
			$("input[name=optionItem]").addClass("readonly");
			$("input[name=optionItem]").attr("readonly",true);
			updateTaskDetail(taskEdit,answerEdit);

		} else {
			//点击编辑
			$("#edit-task-type").show();
			$("#task-type").hide();
			if(count==0) {
				initEdit(editorTaskEdit);
				initEditAnswer(editorAnswerEdit);
				$("#editor-task").attr("contenteditable",true);
				$("#editor-answer").attr("contenteditable",true);
			} else {
				editorTaskEdit.undestroy();
				editorAnswerEdit.undestroy();
			}
			$("#count").val("1");
			document.getElementById("edit-task-btn").value = "保存";

			$(".no-attach").hide();
			$(".taskEdit button").show();
			$(".taskEdit a").hide();
			$("#download-url").hide();
			$("#edit-task-name").attr("contenteditable",true);
			$("#edit-task-name").css("border","0.1rem solid #F1EEEE");
			$("#task-full-mark").attr("contenteditable",true);
			$("#task-full-mark").css("border","0.1rem solid #F1EEEE");
			$("input[name=scoreItem]").removeClass("readonly");
			$("input[name=scoreItem]").attr("readonly",false);
			$("input[name=scoreItemWeight]").removeClass("readonly");
			$("input[name=scoreItemWeight]").attr("readonly",false);
			$("input[name=weight]").removeClass("readonly");
			$("input[name=weight]").attr("readonly",false);
			$("input[name=optionItem]").removeClass("readonly");
			$("input[name=optionItem]").attr("readonly",false);
		}
	});

	function initEdit(editorTaskEdit) {
		editorTaskEdit.config.menus = [
		    'fullscreen',' | ','bold','underline','italic','strikethrough','eraser',
			'forecolor','bgcolor','quote','fontfamily','fontsize','head','unorderlist',
			'orderlist','alignleft','aligncenter','alignright','link','unlink','table',
			'emotion','img','video','insertcode','undo','redo'
	    ];
		editorTaskEdit.config.uploadImgUrl = CONTEXT_PATH + 'resource/editUpload';
		editorTaskEdit.config.uploadImgFileName = "editor-task";
		editorTaskEdit.config.zindex = 20000;
		editorTaskEdit.create();
		$('#editor-task').attr('style','height:auto;');
	}

	function initEditAnswer(editorAnswerEdit) {
		editorAnswerEdit.config.menus = [
		    'fullscreen',' | ','bold','underline','italic','strikethrough','eraser',
			'forecolor','bgcolor','quote','fontfamily','fontsize','head','unorderlist',
			'orderlist','alignleft','aligncenter','alignright','link','unlink','table',
			'emotion','img','video','insertcode','undo','redo'
	    ];
		editorAnswerEdit.config.uploadImgUrl = CONTEXT_PATH + 'resource/editUpload';
		editorAnswerEdit.config.uploadImgFileName = "editor-task";
		editorAnswerEdit.config.zindex = 20000;
		editorAnswerEdit.create();
		$('#editor-answer').attr('style','height:auto;');
	}

	//点击“发布任务”按钮
	$("#publish-task-btn").click(function() {
		//此处需要获取任务类型，根据不同任务类型选择跳转界面
		var taskId=$("input[name='detailTaskId']").val();
		var taskType = $("input[name='hiddenType']").val();
		if(taskType == "PERSONALTASK") {
			//个人任务
			window.location.href=CONTEXT_PATH+"taskSubmitResult/ptListData?taskId="+taskId;
		} else if(taskType == "ASKINLESSON") {
			//课堂提问
			window.location.href=CONTEXT_PATH+"taskAssignView/taskInLessonList?taskId="+taskId;;
		} else {
			//当堂测试、课后作业、实验课题目
			window.location.href=CONTEXT_PATH+"taskAssignView/taskMainList?taskId="+taskId;
		}
	});
});

function taskFileListEdit() {
	var list = $("#task-file-list-edit");
	var obj = document.getElementById("task-file-edit");
	var len = obj.files.length;
	for (var i = 0; i < len; i++) {
		var temp ="";

		temp += ("<li><span>" + obj.files[i].name + "</span>");
		temp += ("<button type=\"button\" style=\"display:block;\" class=\"btn btn-default\">删除</button></li>");

		list.append(temp);
	}
	addAttachment('attach');
}
function answerFileListEdit() {
	var list = $("#answer-file-list-edit");
	var obj = document.getElementById("answer-file-edit");
	var len = obj.files.length;

	for (var i = 0; i < len; i++) {
		var temp = "";

		temp += ("<li><span>" + obj.files[i].name + "</span>");
		temp += ("<button type=\"button\" style=\"display:block\" class=\"btn btn-default\">删除</button></li>");

		list.append(temp);
	}
	addAttachment('answerAttach');
}
$(function() {
	$("#answer-file-list-edit").on("click",".btn",function() {
		$(this).closest("li").remove();
		return false;
	});
	$("#task-file-list-edit").on("click",".btn",function() {
		$(this).closest("li").remove();
		return false;
	});
});
